package com.jumi.microservice.stock.service.impl;

import com.jumi.microservice.common.utils.StringProvider;
import com.jumi.microservice.material.dto.JmUnitResponse;
import com.jumi.microservice.material.dto.warehouse.JmWarehouseResponse;
import com.jumi.microservice.material.service.JmWarehouseService;
import com.jumi.microservice.stock.dto.StockLogResponse;
import com.jumi.microservice.stock.dto.StockLogSearchRequest;
import com.jumi.microservice.stock.mapper.StockLogMapper;
import com.jumi.microservice.stock.service.CommonService;
import com.jumi.microservice.stock.service.StockLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName StockLogServiceImpl
 * @Description
 * @Author Abel Yang
 * @Date 2020/8/24/024 13:23
 **/
@Service
public class StockLogServiceImpl implements StockLogService {

    @Resource
    private JmWarehouseService jmWarehouseService;

    @Resource
    private CommonService commonService;

    @Resource
    private StockLogMapper stockLogMapper;

    @Override
    public List<StockLogResponse> getStockLogList(StockLogSearchRequest jmStockLogSearchRequest) {
        List<StockLogResponse> logList = stockLogMapper.findAll(jmStockLogSearchRequest);
        if (logList.isEmpty()) {
            return logList;
        }

        //单位
        Map<Integer, JmUnitResponse> unitMap = commonService.getUnitMapByIds(logList.stream().map(StockLogResponse::getUnitId).distinct().collect(Collectors.toList()));

        //仓库信息
        Map<Long, String> warehouseMap = jmWarehouseService.getWarehouseList().stream().collect(
                Collectors.toMap(JmWarehouseResponse::getWarehouseId, JmWarehouseResponse::getWarehouseName, (k1, k2) -> k2)
        );
        logList.forEach(stockLog -> {
            JmUnitResponse unitResponse = null != unitMap.get(stockLog.getUnitId()) ? unitMap.get(stockLog.getUnitId()) : new JmUnitResponse();
            stockLog.setUnit(StringProvider.isNotEmpty(unitResponse.getSmunitName()) ? unitResponse.getSmunitName() : "");
            stockLog.setWarehouseName(StringProvider.isNotEmpty(warehouseMap.get(stockLog.getWarehouseId())) ? warehouseMap.get(stockLog.getWarehouseId()) : "");
        });
        return logList;
    }
}
